home *** CD-ROM | disk | FTP | other *** search
/ Cracking 1 / Cracking I..iso / Tools / Ostatní / aPLib v0.26b / src / nasm / depack16.nas < prev    next >
Encoding:
Text File  |  2001-12-15  |  4.3 KB  |  130 lines

  1. ;;
  2. ;; aPLib compression library  -  the smaller the better :)
  3. ;;
  4. ;; NASM 16bit assembler depacker (only for use in assembly code)
  5. ;;
  6. ;; Copyright (c) 1998-2000 by Joergen Ibsen / Jibz
  7. ;; All Rights Reserved
  8. ;;
  9. ;; -> 16bit by METALBRAIN (metalb@bart.us.es)
  10. ;;
  11.  
  12. ;This code is thought to be copypasted directly in your code, at least that's
  13. ;the way I work (I'm not used to libraries, macros, C calling protocols, ...)
  14.  
  15. ;
  16. ;IN:
  17. ;esi= inbuffer  (source)       <-MUST POINT RESERVED MEMORY INSIDE FIRST MEG!
  18. ;edi= outbuffer (destination)  <-MUST POINT RESERVED MEMORY INSIDE FIRST MEG!
  19. ;OUT:
  20. ;edi= uncompressed lenght
  21. ;
  22. ;It uses a lot of registers: EAX, BX, ECX, DX, EDI, ESI, EBP, ES
  23. ;to preserve them, push/pop where indicated
  24.  
  25. _aP_depack_asm:
  26.                 ;Here you can PUSH the registers you're interested to preserve
  27.                 push    edi
  28.                 cld             ;Optional, remove if you're sure of DF=0
  29.                 mov     dl,128
  30. literal:        call    getesi
  31. putedi_nexttag: call    putedi
  32.                 jmp     short nexttag    ;Optional: Instead of unconditional
  33.                                          ; jump, check limit on edi or esi,
  34.                                          ; jump if no error and insert here
  35.                                          ; error code. Limits on edi or esi
  36.                                          ; can also be tested and handled in
  37.                                          ; putedi or getesi.
  38. normalcodepair: xchg    ax,cx
  39.                 dec     ax
  40.                 shl     eax,8
  41.                 call    getesi
  42.                 call    getgamma
  43.                 cmp     eax,32000
  44.                 jae     domatch_with_2inc
  45.                 cmp     ah,5
  46.                 jae     domatch_with_inc
  47.                 cmp     ax,byte 127
  48.                 ja      domatch
  49. domatch_with_2inc:
  50.                 inc     ecx
  51. domatch_with_inc:
  52.                 inc     ecx
  53. domatch:        xchg    eax,ebp
  54. domatch_R0:     mov     eax,ebp
  55. domatch_continue:
  56.                 push    esi
  57.                 mov     esi,edi
  58.                 sub     esi,eax
  59. repmovsb:       call    getesi
  60.                 call    putedi
  61.                 loop    repmovsb,ecx
  62.                 pop     esi
  63. nexttag:        call    getbit
  64.                 jnc     literal
  65.                 xor     ecx,ecx
  66.                 xor     ax,ax
  67.                 call    getbit
  68.                 jnc     codepair
  69.                 call    getbit
  70.                 jnc     shortmatch
  71.                 inc     cx
  72.                 mov     al,16
  73. getmorebits:    call    getbit
  74.                 adc     al,al
  75.                 jnc     getmorebits
  76.                 jnz     domatch_continue
  77.                 jmp     short putedi_nexttag
  78. codepair:       call    getgamma
  79.                 dec     cx
  80.                 loop    normalcodepair
  81.                 push    word domatch_R0
  82. getgamma:       inc     cx
  83. getgammaloop:   call    getbit
  84.                 adc     ecx,ecx
  85.                 call    getbit
  86.                 jc      getgammaloop
  87.                 ret
  88. shortmatch:     call    getesi
  89.                 shr     ax,1
  90.                 jz      donedepacking
  91.                 adc     cx,cx
  92.                 jmp     short domatch_with_2inc
  93. getbit:         add     dl,dl
  94.                 jnz     stillbitsleft
  95.                 xchg    ax,dx
  96.                 call    getesi
  97.                 xchg    ax,dx
  98.                 stc
  99.                 adc     dl,dl
  100. stillbitsleft:  ret
  101. donedepacking:  pop     esi
  102.                 sub     edi,esi
  103.                 ;Here you can POP your registers back
  104.                 ret             ;You can quit this ret and use the routine
  105.                                 ; inline in your code
  106.  
  107. ;
  108. ; This routines deal with 32 bit access through [esi] and [edi]
  109. ;
  110. ; Must be near-accesible from _aP_depack_asm
  111. ;
  112.  
  113. getesi:         push    esi
  114.                 pop     bx
  115.                 pop     bx
  116.                 ror     bx,4
  117.                 mov     es,bx
  118.                 mov     al,[es:si]
  119.                 inc     esi
  120.                 ret
  121. putedi:         push    edi
  122.                 pop     bx
  123.                 pop     bx
  124.                 ror     bx,4
  125.                 mov     es,bx
  126.                 mov     [es:di],al
  127.                 inc     edi
  128.                 xor     eax,eax
  129.                 ret
  130.